package jmind.base.util.reflect;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * description:
 *
 * @author weibo.xie
 * @date:2022/7/8 下午3:03
 */
public class ClassCacheUtil {
    private static  volatile ClassCacheUtil instance;
    public static  ClassCacheUtil getInstance(){
        if(instance==null){
            synchronized (ClassCacheUtil.class){
                if(instance==null){
                    instance=new ClassCacheUtil();
                }
            }
        }
        return instance;
    }

    private  Map<String,Object> cache=new ConcurrentHashMap<>();

    public  <T> T newInstance(Class<T> clazz){
        String name=clazz.getName();
        T o = (T) cache.get(name);
        if(o==null){
            o=ClassUtil.instantiate(clazz);
            cache.put(name,o);
        }
        return o;
    }
 }
